// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");

	var pageIndex = 1;
	var css_autocomplete = require("../../../component/autocomplete/jquery.autocomplete.css");
	var newsCategoryId;
	var init = function() {
		top.init(0);
		router.init();
		$('#recommendList').perfectScrollbar();
		$("#industryInfoPage").addClass("nav_active");
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");
		inquireNewsCategoryPageList();
		inquireNewsPageList();
		inquireNewsPageListAll();
		initEvent();
	};

	var initEvent = function() {


	};

	//资讯栏目列表
	var inquireNewsCategoryPageList = function() {
		base.fnAjax({
			action: "inquireNewsCategoryPageList.json",
			version: "v3.0",
			async: false,
			nocode: true,
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#industryCategoryTpl").html();
					var html = juicer(tpl, data);
					$("#industryCategoryBody").html(html);
					rendering();
					$("#industryCategoryBody a").unbind("click").click(function() {
						$('.infoDetail')[0].style.display = 'none';
						$('.infoList')[0].style.display = 'block';
						$('.infoMore')[0].style.display = 'block';
						$("#industryCategoryBody a").removeClass("active");
						$(this).addClass("active");
						pageIndex = 1;
						$("#newsListBody").html("");
						newsCategoryId = $(this).attr("newsCategoryId");
						inquireNewsPageListAll();
					});
				} else {
					base.alert(3,data.message);
				}
			}
		});
	};

	//推荐资讯信息列表
	var inquireNewsPageList = function() {
		base.fnAjax({
			action: "inquireNewsPageList.json",
			version: "v3.0",
			async: false,
			nocode: true,
			oData: {
				recommend: 1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#recommendListTpl").html();
					var html = juicer(tpl, data);
					$("#recommendListBody").html(html);
					addPoint();
					
					$(".recommendListTitle").unbind("click").click(function() {
						$('.infoDetail')[0].style.display = 'block';
						$('.infoList')[0].style.display = 'none';
						$('.infoMore')[0].style.display = 'none';
					var newsId = 	$(this).attr("id");
					inquireNewsDetail(newsId);
					});
				} else {}
			}
		});
	};

	//推荐资讯信息列表
	var inquireNewsPageListAll = function() {
		base.fnAjax({
			action: "inquireNewsPageList.json",
			async: false,
			nocode: true,
			oData: {
				pageSize: 5,
				pageIndex: pageIndex,
				newsCategoryId: newsCategoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if ((pageIndex - 1) * 5 <= data.totalRecordCount) {
						var tpl = $("#newsListTpl").html();
						var html = juicer(tpl, data);
						$("#newsListBody").append(html);
						pageIndex++;
						$("#showMore").unbind("click").click(function() {
							inquireNewsPageListAll();
						});
						
						$(".infoTitle").unbind("click").click(function() {
						$('.infoDetail')[0].style.display = 'block';
						$('.infoList')[0].style.display = 'none';
						$('.infoMore')[0].style.display = 'none';
					var newsId = 	$(this).attr("id");
					inquireNewsDetail(newsId);
					});
					} else {
					
					}
				} else {
					base.alert($lang,data.message);
				}
			}
		});
	};
	//资讯明细
	var inquireNewsDetail = function(newsId){
		base.fnAjax({
			action: "inquireNewsDetail.json",
			nocode: true,
			oData: {
				newsId:newsId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#newsDetailTpl").html();
						var html = juicer(tpl, data);
						$(".infoDetail").html(html);
					
				} else {
					base.alert($lang,data.message);
				}
			}
		});
	}

	//	推荐资讯前小点
	var addPoint = function() {
		var recommendList = $('.recommendList div div');
		for (var i = 0, j = recommendList.length; i < j; i++) {
			var newHtml = '<img src="../../../res/png/recommand_point.png"/>' + $(recommendList[i]).html();
			$(recommendList[i]).html(newHtml);
		}
		//	推荐信息竖条长度
		$('.recommendBg').css('height', $('.recommendList div div').length * $($('.recommendList div div')[0]).height() + 210 + 'px');

		//				跟随滚动条浮动的div
		$(window).bind("scroll", function() {
			if ($(window).scrollTop() > $('.recommendInfo')[0].offsetTop + $('.recommendInfo').height() - 464) {
				$('.recommendInfo').css({
					'position': 'fixed',
					'top': '-725px'
				});
				$('.divvvvv').css({
					'margin-left': '431px'
				});
			}
			if ($(window).scrollTop() <= 850) {
				$('.recommendInfo').css({
					'position': 'static'
				});
				$('.divvvvv').css({
					'margin-left': '23px'
				});
			}
		});
	}

	var rendering = function() {
		//				滑动导航栏
		var navs = $('#navItems div a');
		var len = 49 * navs.length;
		for (var i = 0, j = navs.length; i < j; i++) {
			len += $(navs[i]).width();
		}
		//				next箭头
		$('.next').click(function() {
			if (navItems.scrollLeft <= (len - 792))
				navItems.scrollLeft += 150;
		});
		$('.previous').click(function() {
			if (navItems.scrollLeft > 0)
				navItems.scrollLeft -= 150;
		});
	};

	return {
		init: init
	};
});